<?php

class Laporan_penjualan_daging extends CI_Controller {

	private $max_stok = 0;
	
	function Laporan_penjualan_daging()
	{
		parent::__construct();	
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
		$this->load->model('laporan/lap_penjualan_daging', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}

		// Load default view
		$data['title'] = "Laporan Penjualan_daging";
		$data['main_view'] = 'laporan/lap_penjualan_daging/lap_penjualan_daging_view';
		
		$data['all_stok'] = $this->get_all_annual_stok();//var_dump($data['all_stok']);
		$data['max_stok'] = $this->max_stok;
		
		$data['stok_prov'] = $this->get_total_permintaan_penjualan_per_prov();
		
		$data['all_stok_titik_penjualan'] = $this->lap_penjualan_daging->get_all_stok_titik_penjualan();
		
		
		// Load view
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		$this->load->view('template', $data);
	}
	
	function set_max_stok($year){
		$result = $this->lap_penjualan_daging->get_max_annual_stok($year);
		if($result > $this->max_stok){
			$this->max_stok = $result;
		}
	}
	
	function laporan_titik_penjualan($id){
		$data['laporan_titik_penjualan'] = $this->lap_penjualan_daging->get_log_titik_penjualan($id);
		
		$this->load->view('laporan/lap_penjualan_daging/laporan_log_titik_penjualan', $data);
	}
	
	function get_total_permintaan_penjualan_per_prov(){
		$aaray = array();
		$all_prov = $this->lokasi_model->get_all_prov();
		foreach($all_prov->result_array() as $prov){
			$tmp = array();
			$tmp['nama_prov'] = $prov['NAMA_PROV'];
			$tmp['penjualan'] = $this->lap_penjualan_daging->get_penjualan_prov($prov['ID_PROV']);
			$tmp['permintaan'] = $this->lap_penjualan_daging->get_permintaan_prov($prov['ID_PROV']);
			
			$array[] = $tmp;
		}
		
		return $array;
	}
	
	function get_all_annual_stok(){
		$array = array();
		$all_year = $this->lap_penjualan_daging->get_all_year();
		if($all_year){
			foreach($all_year->result_array() as $row){
				$tmp = array();
				$res = $this->lap_penjualan_daging->get_annual_stok($row['TAHUN']);
				if($res != null){
					$tmp['monthly_stok'] = $res->result_array();
				}
				
				$tmp['tahun'] = $row;
				$array[] = $tmp;
				
				$this->set_max_stok($row['TAHUN']);
			}
		}//var_dump($array);
		
		return $array;
	}
	
	
}